﻿using MiniExcelLibs;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.超级管理员
{
    public partial class Form发货人员扫码统计 : UIPage
    {
        public Form发货人员扫码统计()
        {
            InitializeComponent();
        }

        private void uiButton查询_Click(object sender, EventArgs e)
        {
          DataTable dt =  查询();
            uiDataGridView1.DataSource = dt;
            uiDataGridView1.AutoSizeColumnsMode= DataGridViewAutoSizeColumnsMode.AllCells;
            uiDataGridView1.Refresh();
        }

        private DataTable 查询()
        {
            string sql = @"select Sys_User.UserTrueName 姓名,count(1) 扫码次数,sum(TRY_CAST(Bus_DeliveryVolume.Weight AS numeric(18,2))) 重量
                            from Bus_DeliveryVolume join Sys_User on Bus_DeliveryVolume.Modifier =Sys_User.UserName 
                            where 1=1 ";
            if (string.IsNullOrWhiteSpace(uiTextBox姓名.Text.Trim()) == false)
            {
                sql += $" and Sys_User.UserTrueName like '%{uiTextBox姓名.Text.Trim()}%' ";
            }

            sql += $"   and Bus_DeliveryVolume.ModifyDate >= '{uiDatePicker开始.Value.ToString("yyyy-MM-dd")}' ";
            sql += $"    and Bus_DeliveryVolume.ModifyDate < '{uiDatePicker结束.Value.AddDays(1).ToString("yyyy-MM-dd")}' ";
            sql += $"      group by  Sys_User.UserTrueName ";
            sql += $"      order by  重量 desc ";
            return pub.GetDataTable(pub.ConnectionString, sql);
        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {
            string path;
            this.saveFileDialog1.Filter = "Excel文件 (*.XLSX)|*.XLSX";
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, 查询(), true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void Form发货人员扫码统计_Load(object sender, EventArgs e)
        {
            this.uiDatePicker开始.Value = DateTime.Now;
            this.uiDatePicker结束.Value  =DateTime.Now;
        }
    }
}
